function [c route temp objectives] = tsp( cities, initial_temperature,...
    cooling_rate,threshold,numberofcitiestoswap, Costs )

it = 0;
temperature = initial_temperature;

complete_temperature_iterations = 0;
temp = zeros(threshold,1);
objectives = zeros(threshold,1);
% This is my objective function, the total distance for the routes.
previous_distance = cost(cities,Costs);
while it < threshold
    
    temp_cities = swapCities(cities,numberofcitiestoswap);
    current_distance = cost(temp_cities,Costs);
    diff = abs(current_distance - previous_distance);
    if current_distance < previous_distance
        cities = temp_cities;

        if complete_temperature_iterations >= 10
            temperature = cooling_rate*temperature;
            complete_temperature_iterations = 0;
        end
        numberofcitiestoswap = round(numberofcitiestoswap...
            *exp(-diff/(it*temperature)));
        if numberofcitiestoswap == 0
            numberofcitiestoswap = 1;
        end
        previous_distance = current_distance;
        it = it + 1;
        temp(it) = temperature;
        objectives(it)=current_distance;
        complete_temperature_iterations = complete_temperature_iterations + 1;
    else
        if rand(1) < exp(-diff/(temperature))
            cities = temp_cities;
            numberofcitiestoswap = round(numberofcitiestoswap...
                *exp(-diff/(it*temperature)));
            if numberofcitiestoswap == 0
                numberofcitiestoswap = 1;
            end
            previous_distance = current_distance;
            complete_temperature_iterations = complete_temperature_iterations + 1;
            it = it + 1;
            temp(it) = temperature;
            objectives(it)=current_distance;
        end
    end
    
end
route = cities;
c = cost(cities, Costs);

